package org.jeecg.modules.base.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.base.entity.BaseBook;
import org.jeecg.modules.base.entity.BaseScore;
import org.jeecg.modules.base.entity.dto.ScoreDto;
import org.jeecg.modules.base.service.IBaseBookService;
import org.jeecg.modules.base.service.IBaseScoreService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: base_score
 * @Author: jeecg-boot
 * @Date:   2021-04-14
 * @Version: V1.0
 */
@Api(tags="base_score")
@RestController
@RequestMapping("/base/baseScore")
@Slf4j
public class BaseScoreController extends JeecgController<BaseScore, IBaseScoreService> {
	@Autowired
	private IBaseScoreService baseScoreService;
	@Autowired
	private IBaseBookService baseBookService;

	 /**
	  * 学生列表查询
	  * @param dto
	  * @return
	  */
	@AutoLog(value = "base_score-学生列表查询")
	@ApiOperation(value="base_score-学生列表查询", notes="base_score-学生列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ScoreDto dto){
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		dto.setUserId(sysUser.getId());
		return Result.OK(baseScoreService.getList(dto));
	}
	 /**
	  * 管理员列表查询
	  * @param dto
	  * @return
	  */
	 @AutoLog(value = "base_score-学生列表查询")
	 @ApiOperation(value="base_score-学生列表查询", notes="base_score-学生列表查询")
	 @GetMapping(value = "/listInfo")
	 public Result<?> queryList(ScoreDto dto){
		 return Result.OK(baseScoreService.getList(dto));
	 }
	
	/**
	 *   添加
	 *
	 * @param baseScore
	 * @return
	 */
	@AutoLog(value = "base_score-添加")
	@ApiOperation(value="base_score-添加", notes="base_score-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BaseScore baseScore) {
		baseScoreService.save(baseScore);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  还书
	 *
	 * @param baseScore
	 * @return
	 */
	@AutoLog(value = "base_score-还书")
	@ApiOperation(value="base_score-还书", notes="base_score-还书")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BaseScore baseScore) {
		BaseScore baseScore1 = baseScoreService.getById(baseScore.getId());
		if(baseScore1.getReturnTime() != null && baseScore1.getNote() != null){
			return Result.error("该书已归还，不可重复还书！");
		}if(baseScore.getNote() == null){
			return Result.error("读书笔记为空，还书失败！");
		}
		if( 100 > baseScore.getNote().length() && 0 <baseScore.getNote().length() ){
			baseScore.setScore(1);
		}else if( 500 > baseScore.getNote().length() && 100<baseScore.getNote().length() ){
			baseScore.setScore(3);
		}else if(500<baseScore.getNote().length()){
			baseScore.setScore(5);
		}
		baseScore.setReturnTime(new Date());
		baseScoreService.updateById(baseScore);
		BaseBook book = baseBookService.getById(baseScore.getId());
		book.setIsBorrow("是");
		baseBookService.updateById(book);
		return Result.OK("还书成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "base_score-通过id删除")
	@ApiOperation(value="base_score-通过id删除", notes="base_score-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		baseScoreService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "base_score-批量删除")
	@ApiOperation(value="base_score-批量删除", notes="base_score-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.baseScoreService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "base_score-通过id查询")
	@ApiOperation(value="base_score-通过id查询", notes="base_score-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BaseScore baseScore = baseScoreService.getById(id);
		if(baseScore==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(baseScore);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param baseScore
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BaseScore baseScore) {
        return super.exportXls(request, baseScore, BaseScore.class, "base_score");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BaseScore.class);
    }

}
